package com.company.project.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.company.project.constant.TradeEnums;
import com.company.project.core.AbstractService;
import com.company.project.dao.TradeCouponMapper;
import com.company.project.model.TradeCoupon;
import com.company.project.protocol.coupon.ChangeCoponStatusReq;
import com.company.project.protocol.coupon.ChangeCoponStatusRes;
import com.company.project.protocol.coupon.QueryCouponReq;
import com.company.project.protocol.coupon.QueryCouponRes;
import com.company.project.service.TradeCouponService;

/**
 * Created by CodeGenerator on 2018/12/07.
 */
@Service
@Transactional
public class TradeCouponServiceImpl extends AbstractService<TradeCoupon> implements TradeCouponService {

	@Resource
	TradeCouponMapper tradeCouponMapper;

	public QueryCouponRes queryCoupon(QueryCouponReq queryCouponReq) {
		QueryCouponRes queryCouponRes = new QueryCouponRes();
		queryCouponRes.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
		queryCouponRes.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());
		try {
			if (queryCouponReq == null || StringUtils.isBlank(queryCouponReq.getCouponId())) {
				throw new Exception("请求参数不正确,优惠券编号为空");
			}
			TradeCoupon tradeCoupon = this.tradeCouponMapper.selectByPrimaryKey(queryCouponReq.getCouponId());
			if (tradeCoupon != null) {
				BeanUtils.copyProperties(tradeCoupon, queryCouponRes);
			} else {
				throw new Exception("未查询到该优惠券");
			}
		} catch (Exception ex) {
			queryCouponRes.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
			queryCouponRes.setRetInfo(ex.getMessage());
		}
		return queryCouponRes;
	}

	public ChangeCoponStatusRes changeCoponStatus(ChangeCoponStatusReq changeCoponStatusReq) {
		ChangeCoponStatusRes changeCoponStatusRes = new ChangeCoponStatusRes();
		changeCoponStatusRes.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
		changeCoponStatusRes.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());
		try {
			if (changeCoponStatusReq == null || StringUtils.isBlank(changeCoponStatusReq.getCouponId())) {
				throw new Exception("请求参数不正确,优惠券编号为空");
			}
			TradeCoupon tradeCoupon = new TradeCoupon();
			tradeCoupon.setCouponId(changeCoponStatusReq.getCouponId());
			tradeCoupon.setOrderId(changeCoponStatusReq.getOrderId());
			tradeCoupon.setIsUsed(changeCoponStatusReq.getIsUsed());
			if (changeCoponStatusReq.getIsUsed().equals(TradeEnums.YesNoEnum.YES.getCode())) {
				// 这里需要注意 sql 语句一定要加 update xx set is_used =1 where is_used =0
				// 防止在并发时 优惠券被多次使用（这里用的 数据库 行锁）
				int reocrd = tradeCouponMapper.useCoupon(tradeCoupon);
				if (reocrd <= 0) {
					throw new Exception("使用优惠券失败");
				}
			} else if (changeCoponStatusReq.getIsUsed().equals(TradeEnums.YesNoEnum.NO.getCode())) {
				// 未使用时 不管其状态如何，反正设置为未使用 就对了
				tradeCouponMapper.unUseCoupon(tradeCoupon);
			}
		} catch (Exception ex) {
			changeCoponStatusRes.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
			changeCoponStatusRes.setRetInfo(ex.getMessage());
		}
		return changeCoponStatusRes;
	}
}
